<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2022-03-26 23:48:06
 */

namespace app\admin\controller;
use app\common\controller\AdminController;

class SystemRule extends AdminController {

    protected function initialize(): void {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '菜单管理', '', 'systemRule');

		$this->ruleModel = model('SystemRule', 'admin');
		$this->categoryDao = model('SystemCategory', 'admin');
	}

	public function index() {
		$map = $parameter = array();

		$cid = input('param.cid', '0', 'intval');
		if ($cid) {
			$map[] = ['cid', '=', $cid];
			$parameter['cid'] = $parameter;
		}
		$this->assign('cid', $cid);

		list($tree, $totalNum) = $this->ruleModel->getTree($map);
		//print_r($tree);
		$this->assign('count', $totalNum);
		$this->assign('pageShow', '');
		$this->assign('tree', $tree);

		//获取菜单分类列表
		list($cateList) = $this->categoryDao->search([]);
		//print_r($cateList);
		$this->assign('cateList', $cateList);

		return $this->fetch();
	}

	public function listorders() {
		$result = parent::baseListorders($this->ruleModel);

		if ($result !== false) {
			$this->success('排序更新成功');
		} else {
			$this->error('排序更新失败');
		}
	}

	public function add() {
		$id = input('param.id', '', 'intval');

		// 获取菜单数据
		if ($id) {
			$info = $this->ruleModel->getInfo($id);
		} else {
			$info = [
				'state' 	=> 1,
				'display'	=> 1
			];
		}
		//print_r($info);
		$this->assign('id', $id);
		$this->assign('info', $info);

		//获取所有的菜单类目
		list($cateList) = $this->categoryDao->search([]);
		//print_r($cateList);
		$this->assign('cateList', $cateList);

		//获取父级菜单
		//list($parentTree) = $this->ruleModel->getParentTree();
		//$this->assign('parentTree', $parentTree);

		//获取父级菜单列表
		list($parentList) = $this->ruleModel->getParentList();
		//print_r($parentList);
		
		//列表转换成树
		$tree_obj = new \org\util\Tree;
		$parentTree = $tree_obj->toFormatTree($parentList, 'name', 'id', 'parentid');
		//print_r($parentTree);
		$this->assign('parentTree', $parentTree);
		
		return $this->fetch();
	}

	public function doAdd() {
		$id = input('post.id', '', 'intval');
		$data = input('post.data/a', '', 'pwEscape');

		if ($id) {
			$info = $this->ruleModel->get($id);
		}

		if (!$data['cid']) {
			$this->error('请选择菜单分类');
		}
		
		$count = $this->ruleModel->countSearch(['parentid'=>$id]);

		$data['has_sub'] = 0;
		$data['is_parent'] = 1;
		if ($id) {
			//检查是否有子菜单
			$count = $this->ruleModel->countSearch(['parentid'=>$id]);
			if ($count) {
				$data['has_sub'] = 1;
			}
		}
		
		if ($data['parentid']) {
			$parentInfo = $this->ruleModel->getInfo($data['parentid']);
			if ($parentInfo['parentid']) {
				$data['level'] = 3;
				$data['is_parent'] = 0;
			} else {
				$data['level'] = 2;
			}
		} else {
			$data['level'] = 1;
		}
		
		//验证数据
		//$this->validateData($data, 'ManageRule.edit');

		if ($id) {
			$result = $this->ruleModel->baseUpdateData($id, $data);
			
			//更新父菜单
			if ($data['parentid']) {
				$this->ruleModel->where('id', $data['parentid'])->update(['has_sub'=>1]);
			}
			
			if ($result !== false) {
				$this->success('保存成功', url('admin/systemRule/index'));
			} else {
				$this->error('保存失败'.showDbError($this->ruleModel));
			}
		} else {
			$result = $this->ruleModel->baseAddData($data);
			
			//更新父菜单
			if ($data['parentid']) {
				$this->ruleModel->where('id', $data['parentid'])->update(['has_sub'=>1]);
			}
			
			if ($result !== false) {
				$this->success('添加成功', url('admin/systemRule/index'));
			} else {
				$this->error('添加失败'.showDbError($this->ruleModel));
			}
		}
	}

	public function doDelete() {
		$id = input('param.id', '', 'intval');

		if ($id) {
			$info = $this->ruleModel->get($id);
		}

		$result = $this->ruleModel->del($id);
		if ($result !== false) {
			$this->success('删除成功', url('admin/authRule/index'));
		} else {
			$this->error('删除失败'.showDbError($this->ruleModel));
		}
	}

	/**
	 * 优化ID(备用，没啥意义)
	 */
	public function optimizePopup() {
		$id = input('param.id', '', 'intval');
		if (!$id) {
			$this->error('非法ID');
		}
		$this->assign('id', $id);

		$html = $this->fetch()->getContent();
		$result = [
			'html' => $html
		];
		$this->success('', '', $result);
	}
	
	public function doOptimizePopup() {
		$id = input('post.id', '', 'intval');
		$newId = input('post.newId', '', 'intval');
		if (!$newId) {
			$this->error('非法ID');
		}
		
		//获取信息
		$authRuleInfo = $this->ruleModel->getInfo($newId);
		if ($authRuleInfo) {
			$this->error('该新ID已经存在，无法处理');
		}
		
		//提现记录状态更新
		$data = [
			'id' => $newId,
		];
		$this->ruleModel->baseUpdateData($id, $data);
		
		$this->success('新ID设置成功', '');
	}
	
	public function ruleCheck() {
		list($authRuleList) = $this->ruleModel->search([]);
		$checkList = [];
		foreach ($authRuleList as $key => $value) {
			if ($value['level'] == 3) {
				$checkInfo = $authRuleList[$value['parentid']];
				if ($checkInfo['level'] != 2) {
					$checkList[] = $value;
				}
				if ($value['is_parent'] == 1) {
					$checkList[] = $value;
				}
			} else if ($value['level'] == 2) {
				$checkInfo = $authRuleList[$value['parentid']];
				if ($checkInfo['level'] != 1) {
					$checkList[] = $value;
				}
			} else if ($value['level'] == 1) {
			} else {
				$checkList[] = $value;
			}
		}
		//echo '发生错误的菜单为：';print_r($checkList);
		$this->assign('checkList', $checkList);

		return $this->fetch();
	}
	
	/**
	 * 根据菜单分类获取父菜单
	 */
	public function getParentByCid() {
		$cid = input('param.cid', '', 'intval');

		//获取父级菜单列表
		list($parentList) = $this->ruleModel->getParentListByCid($cid);
		
		//列表转换成树
		$tree_obj = new \org\util\Tree;
		$parentTree = $tree_obj->toFormatTree($parentList, 'name', 'id', 'parentid');
		//print_r($parentTree);
		
		$resultData = [
			'parentTree' => $parentTree
		];
		$this->success('', '', $resultData);
	}

}